package com.tuanzi.loan.business.repository.collaborate.impl;

import com.tuanzi.loan.business.entity.collaborate.CollaborateChannelRatio;
import com.tuanzi.loan.business.repository.collaborate.custom.CollaborateOldChannelRatioRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.Tables.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.stat.Tables.COLLABORATE_CHANNEL_RATIO;

/**
 * User: liangZongXi
 * Create time: 15:19 2019/5/17
 * Description:
 */

@Repository
public class CollaborateOldChannelRatioRepositoryImpl extends JOOQBaseRepository implements CollaborateOldChannelRatioRepositoryCustom {

    @Override
    public List<CollaborateChannelRatio> findAllIncludeChannelName() {
        fields(COLLABORATE_CHANNEL_RATIO.fields());
        fields(COMM_PRODUCT_CHANNEL.CHANNEL_NAME);
        leftJoin(COMM_PRODUCT_CHANNEL, COLLABORATE_CHANNEL_RATIO.CHANNEL_ID.eq(COMM_PRODUCT_CHANNEL.CHANNEL_CODE));
        return search(new CollaborateChannelRatioMapper());
    }


   private final class CollaborateChannelRatioMapper implements RecordMapper<Record,CollaborateChannelRatio>{

        @Override
        public CollaborateChannelRatio map(Record record) {
            CollaborateChannelRatio ratio = new CollaborateChannelRatio();
            ratio.setId(record.get(COLLABORATE_CHANNEL_RATIO.ID));
            ratio.setChannelId(record.get(COLLABORATE_CHANNEL_RATIO.CHANNEL_ID));
            ratio.setRatio(record.get(COLLABORATE_CHANNEL_RATIO.RATIO));
            ratio.setShowType(record.get(COLLABORATE_CHANNEL_RATIO.SHOW_TYPE));
            //ratio.setTomorrowRatio(record.get(COLLABORATE_CHANNEL_RATIO.TOMORROWRATIO));
            ratio.setChannelName(record.get(COMM_PRODUCT_CHANNEL.CHANNEL_NAME));
            return ratio;
        }
    }

    @Override
    protected TableImpl getTable() {
        return COLLABORATE_CHANNEL_RATIO;
    }
}
